//
//  YWFinishDetailInspectionTableViewController.m
//  maintain
//
//  Created by Kyo on 16/9/26.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWFinishDetailInspectionTableViewController.h"
#import "UIColor+CustomColors.h"
#import "YWCommonTool.h"
#import "NSDate+Additions.h"
#import "YWConst.h"
#import "YWMacro.h"
#import "UIView+Extension.h"
#import "YWInspectionMissionModel.h"
#import "YWInspectionMissionManager.h"
#import "YWInspectionLineRecord.h"
#import "YWScanEquipmentNotMatchVC.h"
#import "YWLoginTool.h"
#import "YWLoginUser.h"
#import "YWHandler.h"
#import "YWQrcodeManager.h"
#import "YWEquipmentManager.h"
//#import "YWInspectionRecordDetailViewController.h"

#import "YWInspectionMisionDetailCell.h"
#import "YWInspectionTimeLineCell.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "Masonry.h"
#import "MJRefresh.h"

#import "YWModifyInspectionMissionTableViewController.h"
#import "MBProgressHUD.h"
#import "FTPopOverMenu.h"
#import "YWInspecEquipmentRecordDetailTableViewController.h"
#import "YWInspecLocationRecordDetailTableViewController.h"

@interface YWFinishDetailInspectionTableViewController ()
@property (nonatomic, assign) BOOL needReload;
@property (nonatomic, strong) NSArray *timeLineDataArray;
@property (nonatomic, weak) MBProgressHUD* HUD;
@end

@implementation YWFinishDetailInspectionTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"inspectionDetail", nil);
    self.view.backgroundColor = [UIColor customBackgroudColor];
//    if (self.inspectionLineRecord.inspectionMission.creator.uniqueId == [YWLoginTool loginUser].uniqueId) {
//        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"• • • " style:UIBarButtonItemStylePlain target:self action:@selector(clickRightBarButtonEvent:event:)];
//    }
    
    [self setupNotifications];
    [self setupTableView];
    
    YWWeakSelf(weakSelf);
    [[YWInspectionMissionManager shareManager] requestInspectionMissionWithMissionId:weakSelf.inspectionLineRecord.inspectionMission.inspectionMissionId Complete:^(YWInspectionMissionModel *inspectionMission) {
        if (inspectionMission) {
            YWInspectionMissionModel *mission = [[YWInspectionMissionManager shareManager]inspectionMissionWithMissionId:inspectionMission.inspectionMissionId];
            weakSelf.inspectionLineRecord.inspectionMission = mission;
            [weakSelf.tableView reloadData];
        }
    }];
}
- (void)setupTableView{
    [self.tableView registerClass:[YWInspectionMisionDetailCell class] forCellReuseIdentifier:YWInspectionMisionDetailCellIdentifier];
    [self.tableView registerClass:[YWInspectionTimeLineCell class] forCellReuseIdentifier:inspectionTimeLineCellIdentifier];
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.estimatedRowHeight = 44.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (void)setupNotifications{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshInspectionMissionTimeLineView) name:KRefreshInspectionTimeLineNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshInspectionMissionTimeLineView) name:KRefreshInspectionLatestRecordNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshInspectionMissionTimeLineView) name:KRefreshModifyInspectionMissionNotification object:nil];
}

- (void)refreshInspectionMissionTimeLineView{
    _needReload = YES;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (_needReload) {
        _needReload = NO;
        _timeLineDataArray = nil;
        [self.tableView reloadData];
    }
}


- (NSArray *)timeLineDataArray{
    if (!_timeLineDataArray) {
        NSArray *array = nil;
        if (self.inspectionLineRecord.inspectionMission.inspectionLine.lineType == YWInspectionLineTypeEquipment) {
            array = [[YWInspectionMissionManager shareManager] timeLineViewDataWithInspectionMission:_inspectionLineRecord.inspectionMission orLineRecord:self.inspectionLineRecord];
        }else{
            array = [[YWInspectionMissionManager shareManager] timeLineViewDataWithLocationMission:_inspectionLineRecord.inspectionMission orLineRecord:self.inspectionLineRecord];
        }
        _timeLineDataArray = array;
    }
    return _timeLineDataArray;
}

#pragma mark - UITableViewDataSource mothods
//- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
//{
//    if (indexPath.row==0) {
//        return [tableView fd_heightForCellWithIdentifier:YWInspectionMisionDetailCellIdentifier cacheByIndexPath:indexPath configuration:^(YWInspectionMisionDetailCell *cell) {
//            cell.fd_enforceFrameLayout = NO;
//            cell.inspectionLineRecord = _inspectionLineRecord;
//        }];
//    }else{
//        return [tableView fd_heightForCellWithIdentifier:inspectionTimeLineCellIdentifier cacheByIndexPath:indexPath configuration:^(YWInspectionTimeLineCell *cell) {
//            cell.fd_enforceFrameLayout = NO;
//            cell.dicData = self.timeLineDataArray[indexPath.row-1];
//        }];
//    }
//}

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1+self.timeLineDataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        YWInspectionMisionDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:YWInspectionMisionDetailCellIdentifier];
        cell.fd_enforceFrameLayout = NO;
        cell.inspectionLineRecord = _inspectionLineRecord;
        return cell;
    }else{
        YWInspectionTimeLineCell *cell =[tableView dequeueReusableCellWithIdentifier:inspectionTimeLineCellIdentifier];
        NSDictionary *dic = self.timeLineDataArray[indexPath.row-1];
        YWInspectionEquipmentRecord *equipmentRecord = [dic objectForKey:YWMissionManagerDicRecordObj];
        if (equipmentRecord) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }else{
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        cell.dicData = dic;
        return cell;
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row>=1) {
        NSDictionary *dic = self.timeLineDataArray[indexPath.row-1];
        YWInspectionEquipmentRecord *equipmentRecord = [dic objectForKey:YWMissionManagerDicRecordObj];
        if (equipmentRecord) {
            if (equipmentRecord.recordType == YWInspectionEquipmentRecordTypeEquipment && equipmentRecord.equipment) {
                YWInspecEquipmentRecordDetailTableViewController *equipmentDetailRecordVC = [[YWInspecEquipmentRecordDetailTableViewController alloc] init];
                equipmentDetailRecordVC.inspectionEquipmentRecord = equipmentRecord;
                [self.navigationController pushViewController:equipmentDetailRecordVC animated:YES];
            }else{
                YWInspecLocationRecordDetailTableViewController *locationRecordVC = [[YWInspecLocationRecordDetailTableViewController alloc] init];
                locationRecordVC.inspectionEquipmentRecord = equipmentRecord;
                [self.navigationController pushViewController:locationRecordVC animated:YES];
            }
        }
    }
}

//- (void)clickRightBarButtonEvent:(UIBarButtonItem *)sender event:(UIEvent *)event{
//    [FTPopOverMenu showFromEvent:event
//                        withMenu:@[@"修改",@"删除"]
//                  imageNameArray:@[@"menu_edit",@"menu_delete"]
//                       doneBlock:^(NSInteger selectedIndex) {
//                           if (selectedIndex==0) {
//                               YWModifyInspectionMissionTableViewController *modifyInspectionVC = [[YWModifyInspectionMissionTableViewController alloc] init];
//                               modifyInspectionVC.inspectionMission = _inspectionMissionDataSource;
//                               [self.navigationController pushViewController:modifyInspectionVC animated:YES];
//                           }else{
//                               [self.HUD show:YES];
//                               [[YWInspectionMissionManager shareManager] requestDeleteInspectionMission:_inspectionMissionDataSource.inspectionMissionId completeBlock:^(BOOL success) {
//                                   [self.HUD hide:YES];
//                                   dispatch_async(dispatch_get_main_queue(), ^{
//                                       if (success) {
//                                           [self.navigationController popViewControllerAnimated:YES];
//                                       }else{
//                                           [YWCommonTool showTextHUDWithContent:@"删除失败,请稍后重试"];
//                                       }
//                                   });
//                               }];
//                           }
//                       } dismissBlock:nil];
//}

- (MBProgressHUD*)HUD
{
    if (!_HUD) {
        MBProgressHUD* hud = [[MBProgressHUD alloc] initWithView:[UIApplication sharedApplication].keyWindow];
        hud.labelFont = kHUDLabelFont;
        hud.labelText = NSLocalizedString(kTextBeingProcessed, nil);
        hud.dimBackground = YES;
        //hud.animationType = MBProgressHUDAnimationZoom;
        _HUD = hud;
        _HUD.tag = kMBProgressHUDTag;
        [self.navigationController.view addSubview:hud];
    }
    return _HUD;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
